<?php

//decode by http://www.yunlu99.com/
define('KOD_GROUP_PATH', '{groupPath}');
define('KOD_GROUP_SHARE', '{groupShare}');
define('KOD_USER_SELF', '{userSelf}');
define('KOD_USER_SHARE', '{userShare}');
define('KOD_USER_RECYCLE', '{userRecycle}');
define('KOD_USER_FAV', '{userFav}');
define('KOD_GROUP_ROOT_SELF', '{treeGroupSelf}');
define('KOD_GROUP_ROOT_ALL', '{treeGroupAll}');
function _DIR_CLEAR($_var_0)
{
	$_var_0 = str_replace('\\', '/', trim($_var_0));
	$_var_0 = preg_replace('/\\/+/', '/', $_var_0);
	$_var_1 = $_var_0;
	if (isset($GLOBALS['isRoot']) && $GLOBALS['isRoot']) {
		return $_var_0;
	}
	$_var_2 = '/../';
	if (substr($_var_0, 0, 3) == '../') {
		$_var_0 = substr($_var_0, 3);
	}
	while (strstr($_var_0, $_var_2)) {
		$_var_0 = str_replace($_var_2, '/', $_var_0);
	}
	$_var_0 = preg_replace('/\\/+/', '/', $_var_0);
	return $_var_0;
}
function _DIR($_var_3)
{
	$_var_4 = _DIR_CLEAR($_var_3);
	$_var_4 = iconv_system($_var_4);
	$_var_5 = array(KOD_GROUP_PATH, KOD_GROUP_SHARE, KOD_USER_SELF, KOD_GROUP_ROOT_SELF, KOD_GROUP_ROOT_ALL, KOD_USER_SHARE, KOD_USER_RECYCLE, KOD_USER_FAV);
	$GLOBALS['kodPathType'] = '';
	$GLOBALS['kodPathPre'] = HOME;
	$GLOBALS['kodPathId'] = '';
	unset($GLOBALS['kodPathIdShare']);
	foreach ($_var_5 as $_var_6) {
		if (substr($_var_4, 0, strlen($_var_6)) == $_var_6) {
			$GLOBALS['kodPathType'] = $_var_6;
			$_var_7 = explode('/', $_var_4);
			$_var_8 = $_var_7[0];
			unset($_var_7[0]);
			$_var_9 = implode('/', $_var_7);
			$_var_10 = explode(':', $_var_8);
			if (count($_var_10) > 1) {
				$GLOBALS['kodPathId'] = trim($_var_10[1]);
			} else {
				$GLOBALS['kodPathId'] = '';
			}
			break;
		}
	}
	switch ($GLOBALS['kodPathType']) {
		case '':
			$_var_4 = iconv_system(HOME) . $_var_4;
			break;
		case KOD_USER_RECYCLE:
			$GLOBALS['kodPathPre'] = trim(USER_RECYCLE, '/');
			$GLOBALS['kodPathId'] = '';
			return iconv_system(USER_RECYCLE) . '/' . str_replace(KOD_USER_RECYCLE, '', $_var_4);
		case KOD_USER_SELF:
			$GLOBALS['kodPathPre'] = trim(HOME_PATH, '/');
			$GLOBALS['kodPathId'] = '';
			return iconv_system(HOME_PATH) . '/' . str_replace(KOD_USER_SELF, '', $_var_4);
		case KOD_USER_FAV:
			$GLOBALS['kodPathPre'] = trim(KOD_USER_FAV, '/');
			$GLOBALS['kodPathId'] = '';
			return KOD_USER_FAV;
		case KOD_GROUP_ROOT_SELF:
			$GLOBALS['kodPathPre'] = trim(KOD_GROUP_ROOT_SELF, '/');
			$GLOBALS['kodPathId'] = '';
			return KOD_GROUP_ROOT_SELF;
		case KOD_GROUP_ROOT_ALL:
			$GLOBALS['kodPathPre'] = trim(KOD_GROUP_ROOT_ALL, '/');
			$GLOBALS['kodPathId'] = '';
			return KOD_GROUP_ROOT_ALL;
		case KOD_GROUP_PATH:
			$_var_11 = systemGroup::getInfo($GLOBALS['kodPathId']);
			if (!$GLOBALS['kodPathId'] || !$_var_11) {
				return false;
			}
			owner_group_check($GLOBALS['kodPathId']);
			$GLOBALS['kodPathPre'] = group_home_path($_var_11);
			$_var_4 = iconv_system($GLOBALS['kodPathPre']) . $_var_9;
			break;
		case KOD_GROUP_SHARE:
			$_var_11 = systemGroup::getInfo($GLOBALS['kodPathId']);
			if (!$GLOBALS['kodPathId'] || !$_var_11) {
				return false;
			}
			owner_group_check($GLOBALS['kodPathId']);
			$GLOBALS['kodPathPre'] = group_home_path($_var_11) . 'share/';
			$_var_4 = iconv_system($GLOBALS['kodPathPre']) . $_var_9;
			break;
		case KOD_USER_SHARE:
			$_var_11 = systemMember::getInfo($GLOBALS['kodPathId']);
			if (!$GLOBALS['kodPathId'] || !$_var_11) {
				return false;
			}
			if ($GLOBALS['kodPathId'] != $_SESSION['kodUser']['userID']) {
				$_var_12 = $GLOBALS['config']['pathRoleGroupDefault']['1']['actions'];
				path_role_check($_var_12);
			}
			$GLOBALS['kodPathPre'] = '';
			$GLOBALS['kodPathIdShare'] = $_var_3;
			if ($_var_9 == '') {
				return $_var_4;
			} else {
				$_var_13 = explode('/', $_var_9);
				$_var_13[0] = iconv_app($_var_13[0]);
				$_var_14 = systemMember::userShareGet($GLOBALS['kodPathId'], $_var_13[0]);
				$GLOBALS['kodShareInfo'] = $_var_14;
				$GLOBALS['kodPathIdShare'] = KOD_USER_SHARE . ':' . $GLOBALS['kodPathId'] . '/' . $_var_13[0] . '/';
				unset($_var_13[0]);
				if (!$_var_14) {
					return false;
				}
				$_var_15 = rtrim($_var_14['path'], '/') . '/' . iconv_app(implode('/', $_var_13));
				if ($_var_11['role'] != '1') {
					$_var_16 = user_home_path($_var_11);
					$GLOBALS['kodPathPre'] = $_var_16 . rtrim($_var_14['path'], '/') . '/';
					$_var_4 = $_var_16 . $_var_15;
				} else {
					$GLOBALS['kodPathPre'] = $_var_14['path'];
					$_var_4 = $_var_15;
				}
				if ($_var_14['type'] == 'file') {
					$GLOBALS['kodPathIdShare'] = rtrim($GLOBALS['kodPathIdShare'], '/');
					$GLOBALS['kodPathPre'] = rtrim($GLOBALS['kodPathPre'], '/');
				}
				$_var_4 = iconv_system($_var_4);
			}
			break;
		default:
			break;
	}
	if ($_var_4 != '/') {
		$_var_4 = rtrim($_var_4, '/');
		if (is_dir($_var_4)) {
			$_var_4 = $_var_4 . '/';
		}
	}
	return $_var_4;
}
function _DIR_OUT($_var_17)
{
	if (is_array($_var_17)) {
		foreach ($_var_17['fileList'] as $_var_18 => &$_var_19) {
			$_var_19['path'] = preClear($_var_19['path']);
		}
		foreach ($_var_17['folderList'] as $_var_18 => &$_var_19) {
			$_var_19['path'] = preClear(rtrim($_var_19['path'], '/') . '/');
		}
	} else {
		$_var_17 = preClear($_var_17);
	}
	return $_var_17;
}
function preClear($_var_20)
{
	$_var_21 = $GLOBALS['kodPathType'];
	$_var_22 = rtrim($GLOBALS['kodPathPre'], '/');
	$_var_23 = array(KOD_USER_FAV, KOD_GROUP_ROOT_SELF, KOD_GROUP_ROOT_ALL);
	if (isset($GLOBALS['kodPathType']) && in_array($GLOBALS['kodPathType'], $_var_23)) {
		return $_var_20;
	}
	if (ST == 'share') {
		return str_replace($_var_22, '', $_var_20);
	}
	if ($GLOBALS['kodPathId'] != '') {
		$_var_21 .= ':' . $GLOBALS['kodPathId'] . '/';
	}
	if (isset($GLOBALS['kodPathIdShare'])) {
		$_var_21 = $GLOBALS['kodPathIdShare'];
	}
	$_var_24 = $_var_21 . str_replace($_var_22, '', $_var_20);
	$_var_24 = str_replace('//', '/', $_var_24);
	return $_var_24;
}
include CLASS_DIR . '.cache_data';
function owner_group_check($_var_25)
{
	if (!$_var_25) {
		show_json(LNG('group_not_exist') . $_var_25, false);
	}
	if ($GLOBALS['isRoot'] || isset($GLOBALS['kodPathAuthCheck']) && $GLOBALS['kodPathAuthCheck'] === true) {
		return;
	}
	$_var_26 = systemMember::userAuthGroup($_var_25);
	if ($_var_26 == false) {
		if ($GLOBALS['kodPathType'] == KOD_GROUP_PATH) {
			show_json(LNG('no_permission_group'), false);
		} else {
			if ($GLOBALS['kodPathType'] == KOD_GROUP_SHARE) {
				$_var_27 = $GLOBALS['config']['pathRoleGroupDefault']['1'];
			}
		}
	} else {
		$_var_27 = $GLOBALS['config']['pathRoleGroup'][$_var_26];
	}
	path_role_check($_var_27['actions']);
}
function path_role_check($_var_28)
{
	if ($GLOBALS['isRoot'] || isset($GLOBALS['kodPathAuthCheck']) && $GLOBALS['kodPathAuthCheck'] === true) {
		return;
	}
	$_var_29 = role_permission_arr($_var_28);
	$GLOBALS['kodPathRoleGroupAuth'] = $_var_29;
	if (!isset($_var_29[ST . '.' . ACT]) && ST != 'share') {
		show_json(LNG('no_permission_action'), false);
	}
}
function role_permission_arr($_var_30)
{
	$_var_31 = array();
	$_var_32 = $GLOBALS['config']['pathRoleDefine'];
	foreach ($_var_30 as $_var_33 => $_var_34) {
		if (!$_var_34) {
			continue;
		}
		$_var_35 = explode(':', $_var_33);
		if (count($_var_35) == 2 && is_array($_var_32[$_var_35[0]]) && is_array($_var_32[$_var_35[0]][$_var_35[1]])) {
			$_var_31 = array_merge($_var_31, $_var_32[$_var_35[0]][$_var_35[1]]);
		}
	}
	$_var_36 = array();
	foreach ($_var_31 as $_var_34) {
		$_var_36[$_var_34] = '1';
	}
	return $_var_36;
}
function check_file_writable_user($_var_37)
{
	if (!isset($GLOBALS['kodPathType'])) {
		_DIR($_var_37);
	}
	$_var_38 = 'editor.fileSave';
	if ($GLOBALS['isRoot']) {
		return @is_writable($_var_37);
	}
	if ($GLOBALS['auth'][$_var_38] != '1') {
		return false;
	}
	if ($GLOBALS['kodPathType'] == KOD_GROUP_PATH && is_array($GLOBALS['kodPathRoleGroupAuth']) && $GLOBALS['kodPathRoleGroupAuth'][$_var_38] == '1') {
		return true;
	}
	if ($GLOBALS['kodPathType'] == '' || $GLOBALS['kodPathType'] == KOD_USER_SELF) {
		return true;
	}
	return false;
}
function space_size_use_check()
{
	if (!system_space()) {
		return;
	}
	if ($GLOBALS['isRoot'] == 1) {
		return;
	}
	if (isset($GLOBALS['kodBeforePathId']) && isset($GLOBALS['kodPathId']) && $GLOBALS['kodBeforePathId'] == $GLOBALS['kodPathId']) {
		return;
	}
	if ($GLOBALS['kodPathType'] == KOD_GROUP_SHARE || $GLOBALS['kodPathType'] == KOD_GROUP_PATH) {
		systemGroup::spaceCheck($GLOBALS['kodPathId']);
	} else {
		if (ST == 'share') {
			$_var_39 = $GLOBALS['in']['user'];
		} else {
			$_var_39 = $_SESSION['kodUser']['userID'];
		}
		systemMember::spaceCheck($_var_39);
	}
}
function spaceSizeChange($_var_40, $_var_41 = true, $_var_42 = false, $_var_43 = false)
{
	if (!system_space()) {
		return;
	}
	if ($_var_42 === false) {
		$_var_42 = $GLOBALS['kodPathType'];
		$_var_43 = $GLOBALS['kodPathId'];
	}
	$_var_41 = $_var_41 ? 1 : -1;
	if (is_file($_var_40)) {
		$_var_44 = get_filesize($_var_40);
	} else {
		if (is_dir($_var_40)) {
			$_var_45 = _path_info_more($_var_40);
			$_var_44 = $_var_45['size'];
		} else {
			return;
		}
	}
	if ($_var_42 == KOD_GROUP_SHARE || $_var_42 == KOD_GROUP_PATH) {
		systemGroup::spaceChange($_var_43, $_var_44 * $_var_41);
	} else {
		if (ST == 'share') {
			$_var_46 = $GLOBALS['in']['user'];
		} else {
			$_var_46 = $_SESSION['kodUser']['userID'];
		}
		systemMember::spaceChange($_var_46, $_var_44 * $_var_41);
	}
}
function spaceSizeChange_move($_var_47)
{
	if (isset($GLOBALS['kodBeforePathId']) && isset($GLOBALS['kodPathId'])) {
		if ($GLOBALS['kodBeforePathId'] == $GLOBALS['kodPathId']) {
			return;
		} else {
			spaceSizeChange($_var_47);
			spaceSizeChange($_var_47, false, $GLOBALS['beforePathType'], $GLOBALS['kodBeforePathId']);
		}
	} else {
		spaceSizeChange($_var_47);
	}
}
function space_size_use_reset()
{
	if (!system_space()) {
		return;
	}
	$_var_48 = isset($GLOBALS['kodPathType']) ? $GLOBALS['kodPathType'] : '';
	$_var_49 = isset($GLOBALS['kodPathId']) ? $GLOBALS['kodPathId'] : '';
	if ($_var_48 == KOD_GROUP_SHARE || $_var_48 == KOD_GROUP_PATH) {
		systemGroup::spaceChange($_var_49);
	} else {
		$_var_50 = $_SESSION['kodUser']['userID'];
		systemMember::spaceChange($_var_50);
	}
}
function init_space_size_hook()
{
	Hook::bind('uploadFileBefore', 'space_size_use_check');
	Hook::bind('uploadFileAfter', 'spaceSizeChange');
	Hook::bind('explorer.serverDownloadBefore', 'space_size_use_check');
	Hook::bind('explorer.unzipBefore', 'space_size_use_check');
	Hook::bind('explorer.zipBefore', 'space_size_use_check');
	Hook::bind('explorer.pathCopy', 'space_size_use_check');
	Hook::bind('explorer.mkfileBefore', 'space_size_use_check');
	Hook::bind('explorer.mkdirBefore', 'space_size_use_check');
	Hook::bind('explorer.pathMove', 'space_size_use_check');
	Hook::bind('explorer.mkfileAfter', 'spaceSizeChange');
	Hook::bind('explorer.pathCopyAfter', 'spaceSizeChange');
	Hook::bind('explorer.unzipAfter', 'spaceSizeChange');
	Hook::bind('explorer.serverDownloadAfter', 'spaceSizeChange');
	Hook::bind('explorer.pathMoveBefore', 'space_size_use_check');
	Hook::bind('explorer.pathMoveBfter', 'spaceSizeChange_move');
	Hook::bind('explorer.pathRemoveAfter', 'space_size_use_reset');
}
function init_session()
{
	if (!function_exists('session_start')) {
		show_tips('服务器php组件缺失! (PHP miss lib)<br/>请检查php.ini，需要开启模块: <br/><pre>session,json,curl,exif,mbstring,ldap,gd,pdo,pdo-mysql,xml</pre><br/>');
	}
	if (isset($_GET['accessToken'])) {
		access_token_check($_GET['accessToken']);
	} else {
		if (isset($_GET['access_token'])) {
			access_token_check($_GET['access_token']);
		} else {
			@session_name(SESSION_ID);
		}
	}
	$_var_51 = @session_save_path();
	if (class_exists('SaeStorage') || defined('SAE_APPNAME') || defined('SESSION_PATH_DEFAULT') || @ini_get('session.save_handler') != 'files' || isset($_SERVER['HTTP_APPNAME'])) {
	} else {
		chmod_path(KOD_SESSION, 0777);
		@session_save_path(KOD_SESSION);
	}
	@session_start();
	$_SESSION['kod'] = 1;
	@session_write_close();
	unset($_SESSION);
	@session_start();
	if (!$_SESSION['kod']) {
		@session_save_path($_var_51);
		@session_start();
		$_SESSION['kod'] = 1;
		@session_write_close();
		unset($_SESSION);
		@session_start();
	}
	if (!$_SESSION['kod']) {
		show_tips('服务器session写入失败! (session write error)<br/>' . '请检查php.ini相关配置,查看磁盘是否已满,或咨询服务商。<br/><br/>' . 'session.save_path=' . $_var_51 . '<br/>' . 'session.save_handler=' . @ini_get('session.save_handler') . '<br/>');
	}
}
function access_token_check($_var_52)
{
	$_var_53 = $GLOBALS['config']['settingSystem']['systemPassword'];
	$_var_53 = substr(md5('kodExplorer_' . $_var_53), 0, 15);
	$_var_54 = Mcrypt::decode($_var_52, $_var_53);
	if (!$_var_54) {
		show_tips('accessToken error!');
	}
	if ($_COOKIE[SESSION_ID] && $_COOKIE[SESSION_ID] !== $_var_54) {
		@session_name(SESSION_ID);
		return;
	}
	session_id($_var_54);
}
function access_token_get()
{
	$_var_55 = session_id();
	$_var_56 = $GLOBALS['config']['settingSystem']['systemPassword'];
	$_var_56 = substr(md5('kodExplorer_' . $_var_56), 0, 15);
	$_var_57 = Mcrypt::encode($_var_55, $_var_56, 3600 * 24);
	return $_var_57;
}
function init_config()
{
	init_setting();
	init_session();
	init_space_size_hook();
}